<?php
namespace plugins\order\controller;
use cmf\controller\PluginAdminBaseController;
use plugins\member\model\CommModel;
use plugins\member\model\MemberModel;
use think\Db;
use think\Request;
use app\common\lib\model_operate\Transit;
use plugins\order\model\OrderModel;
use plugins\finance\model\FinanceModel;
use plugins\pay\lib\Pay;
use plugins\order\model\OrderRefundModel;
class AuditOrderController extends PluginAdminBaseController
{
	//申请退款列表
	public function order_refund_list()
	{
		$param = $this->request->param();
		//流水记录
		$search =  Transit::createTags('search');
		$search->setParam($param);
		$table = Transit::createTags('table');
		$table->setParam($search);
		$pagesize = isset( $param [ 'pagesize' ] ) ? $param [ 'pagesize' ] : 20;
		$sear = [];
		//权限检测
		$check = ( new \plugins\store\Common )->getAllowAccess();
		if($check === true){
			// $where = ['store_id'=>'admin'];
			$store_id = 'admin';
		}else{
			// $where = ['store_id'=>$check];
			$store_id = $check;
		}
        //流水记录列表
		$list = $table->query()
		        ->order('id desc')
		        ->where(['order_refund'=>['neq',4],'store_id'=>$store_id])
                ->paginates($pagesize,false,['query'=>$sear]);
		$this->assign('search',$search);
		$this->assign('table',$table);
		$this->assign('list',$list);
		return $this->fetch();
	}
	//同意订单退款
	public function order_refund_agree()
	{
		$param = $this->request->param();
		//权限检测
		$check = ( new \plugins\store\Common )->getAllowAccess();
		if($check === true){
			$check = 'admin';
			$finance_type = 'store';
		}else{
			$finance_type = 'store';
			$where['store_id'] = $check;
		}
		$where['order_num'] = ['in',$param['order_num']];
		//所选内容
		$list = ( new OrderModel )
				->where($where)
				->select();
		//遍历结果
		foreach ($list as $key => $value) {
			if($value->order_refund != 2){
				continue;
			}

			$item_where =  " order_num like '%".$value->order_num."%' ";
            $order_member = Db::name('order_member')->where($item_where)->find();
            if(empty($order_member)){
                return $this->error('未找到订单!');
            }

			//调用pay创建退款
            $type = 'wechatpay' ; //wechatpay为微信支付方式标识
            $wechatpay= Pay::createInstance(  $type  );	//创建pay的class
            $bizcontent['out_trade_no'] = $order_member['order_member_num'];
            $bizcontent['out_refund_no'] = $order_member['order_member_num'];
//            $bizcontent['total_fee'] = $value->order_money * 100;
//            $bizcontent['refund_fee'] = $value->order_money * 100;
            $bizcontent['total_fee'] = 0.01 * 100;
            $bizcontent['refund_fee'] = 0.01 * 100;
            $bizcontent = json_encode($bizcontent);
            $res = $wechatpay->refund( $bizcontent ) ;
            if($res['result_code']=='SUCCESS'){
                $update['order_status'] = 5;
                $update['order_refund'] = 1;
                Db::name('order')->where('order_num',$value->order_num)->update($update);

                //同步APP
                $commModel =  new CommModel();
                $member_model = new MemberModel();
                $member = $member_model->where('uid',$value->member_id)->find();
                $order_param['user_id'] = $member['user_id'];
                $order_param['order_no'] = $value->order_num;
                $order_param['status'] = 5;
                $commModel->orderStatusSyn($order_param);
            }else{
                return $this->error($res['err_code_des']);
            }
		}
		return $this->success('操作成功,选中的待退款申请已通过');
	}
	//拒绝订单退款
	public function order_refund_refuse()
	{
		$param = $this->request->param();
		//权限检测
		$check = ( new \plugins\store\Common )->getAllowAccess();
		if($check === true){

		}else{
			$where['store_id'] = $check;
		}
		$where['order_num'] = ['in',$param['order_num']];
		//所选内容
		$list = ( new OrderModel )
				->where($where)
				->select();
		foreach ($list as $key => $value) {
			if($value->order_refund != 2){
				continue;
			}
			//得到用户的退款申请
			$refund = ( new OrderRefundModel )->get(['order_num'=>$value->order_num]);
			$refund->save(['store_remark'=>$param['reason'],'store_time'=>date('Y-m-d H:i:s')]);
			$value->order_refund = 3;
			$value->order_status = $value->before_status;
			$value->save();
		}
		return $this->success('操作成功,选中的订单退款申请已拒绝');
	}
}